package com.lyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyz.domain.dto.UserLoginDTO;
import com.lyz.domain.dto.UserRegisterDTO;
import com.lyz.domain.entity.User;
import com.lyz.mapper.UserMapper;
import com.lyz.service.IBookService;
import com.lyz.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserService implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public boolean login(UserLoginDTO userLogin) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
                .eq(User::getNumber, userLogin.getNumber())
                .eq(User::getPassword, userLogin.getPassword());
        User user = userMapper.selectOne(queryWrapper);
        return user != null;
    }

    @Override
    public boolean register(UserRegisterDTO userRegister) {
        /* 判断账号(number)是否重复 */
        boolean exists = userMapper.selectOne(
                new LambdaQueryWrapper<User>()
                        .eq(User::getNumber, userRegister.getNumber())) != null;
        if (exists)
            return false;
        User user = new User();
        BeanUtils.copyProperties(userRegister, user);
        user.setGmtCreate(LocalDateTime.now());
        user.setGmtModified(LocalDateTime.now());
        int res = userMapper.insert(user);
        return res > 0;
    }

    @Override
    public boolean logout() {
        return false;
    }

    @Override
    public List<User> queryAll() {
        return userMapper.selectList(null);
    }
}
